$(function() {
    $("label.panel-header").collapsiblePanel({
                minimize: function() {
                    $(this).hide();
                },
                restore: function() {
                    $(this).show();
                }
            });

    $("#replicateWorkspace").click(replicateWorkspace);
    $("#cleanWorkspace").click(cleanWorkspace);
    $("#exportWorkspace").click(exportWorkspace);
    $("#importWorkspace").click(importWorkspace);
    $("#clearReferenceDay").click(clearReferenceDay);

    $("#replicateWorkspaceForm").validate({
                rules: {
                    intervalStart: "required",
                    intervalStop: "required",
                    replicateDate: "required"
                },
                messages: {
                    intervalStart: i18n.workspace.validation.replicateIntervalStart,
                    intervalStop: i18n.workspace.validation.replicateIntervalStop,
                    replicateDate: i18n.workspace.validation.replicateDateRequired
                },
                errorElement: "div",
                errorLabelContainer: $("#replicateWorkspaceFormErrs"),
                submitHandler: function() {
                    var sourceWs = $("#sourceWorkspace :selected").val();
                    var targetWs = $("#targetWorkspace :selected").val();
                    var start = $("#intervalStart").val();
                    var stop = $("#intervalStop").val();
                    var replDate = $("#replicateDate").val();
                    var type = $("#scheduleType :selected").val();
                    var refDay = $("#referenceDay").val();

                    WorkspaceServiceRemote.doReplication(sourceWs, targetWs, start,
                            stop, replDate, type, refDay, {
                                callback : function(success) {
                                    alert(success ? i18n.workspace.replicationSuccess :
                                            i18n.workspace.replicationFail);
                                },timeout : 280000
                            }
                    )
                }
            });

    $('#intervalStart').change(function() {
        immediateValidation(this);
    });

    $('#intervalStop').change(function() {
        immediateValidation(this);
    });

    $('#replicateDate').change(function() {
        immediateValidation(this);
    });
});

function replicateWorkspace() {
    if ($("#intervalStart").val() > $("#intervalStop").val()) {
        showAlertDialog($("#wrongReplicationIntervalDialog"), null, 400, 150);
        return;
    }

    $("#replicateWorkspaceForm").submit();
}

function exportWorkspace() {
    var workspace = $("#savecaseWorkspace :selected").val();

    WorkspaceServiceRemote.getSavecase(workspace,
            {callback: function(data) {
                if (data == null) {
                    alert("Savecase generation failed. Try again later");
                } else {
                    dwr.engine.openInDownload(data);
                }
            }
            })
}

function importWorkspace() {
    var file = dwr.getValue('savecaseFile');
    var workspaceCode = $("#targetSavecaseWorkspace :selected").val();

    WorkspaceServiceRemote.loadSavecase(workspaceCode, file,
            {callback: function(result) {
                if (result) {
                    alert("Savecase loaded successfully");
                } else {
                    alert("Savecase loading failed. Try again later");
                }
            },timeout:600000
            })
}

function cleanWorkspace() {
    var workspaceCode = $("#workspaceToClean :selected").val();
    var scheduleTypeCode = $("#scheduleTypeToClean :selected").val();

    WorkspaceServiceRemote.cleanWorkspace(workspaceCode, scheduleTypeCode,
            {callback: function(result) {
                if (result) {
                    alert("Clean up was made successfully");
                } else {
                    alert("Clean up failed");
                }
            }
            });
}

function clearReferenceDay() {
    $("#referenceDay").val('')
}

function immediateValidation(field) {
    $("#replicateWorkspaceForm").validate().element(field);
}